# -*- coding: utf-8 -*-
# created on 2016/9/23


from mathsolver.functions.base import *
from sympy import Abs, sqrt, trigsimp, simplify
from sympy.abc import x, y, d
from mathsolver.functions.zhixian.base import point_to_line_dis2
from mathsolver.functions.zhixian.property import GetZhiXianCoeff


# 求点到直线的距离
class DianToXianJuLi(BaseFunction):
    """
    点P(-1,2)到直线8x-6y+15=0的距离为()
    """
    def solver(self, *args):
        point = args[0].sympify()
        eqs = args[1].sympify()
        if len(eqs) == 2 and isinstance(eqs, (list, tuple)):
            expr = (eqs[0] - eqs[1]).expand().simplify()
            a, b, c = GetZhiXianCoeff().solver(args[1]).output[0].sympify()
            juli = Abs(expr.subs({x: point[0], y: point[1]})) / (sqrt(a ** 2 + b ** 2))
            juli = trigsimp(juli)
            self.steps.append(["", "由点到直线的距离公式知，%s到%s的距离为" % (BasePoint({"name": "", "value": point}).printing(), BaseEq(eqs).printing())])
            self.steps.append(["", BaseEq([d, juli]).printing()])
            self.output.append(BasePoly(juli))
            self.label.add("求点到直线的距离")
            return self
        else:
            julis = []
            for eq in eqs:
                expr = (eq[0] - eq[1]).expand().simplify()
                a, b, c = GetZhiXianCoeff().solver(BaseEq([expr, S.Zero])).output[0].sympify()
                juli = Abs(expr.subs({x: point[0], y: point[1]})) / (sqrt(a ** 2 + b ** 2))
                juli = trigsimp(juli)
                self.steps.append(["", "由点到直线的距离公式知，%s到%s的距离为" % (BasePoint({"name": "", "value": point}).printing(), BaseEq(eq).printing())])
                self.steps.append(["", BaseEq([d, juli]).printing()])
                julis.append(juli)
            self.output.append(BaseValues(julis))
            self.label.add("求点到多直线的距离")
            return self


# 求点到直线的距离的平方
class DianToXianJuLi2(BaseFunction):
    def solver(self, *args):
        point = args[0].sympify()
        yibanshi = args[1].sympify()
        expr = yibanshi[0]
        a, b, c = GetZhiXianCoeff().solver(args[1]).output[0].sympify()
        juli = Abs(expr.subs({x: point[0], y: point[1]})) / (sqrt(a ** 2 + b ** 2))
        juli = trigsimp(juli)
        self.steps.append(["", "由点到直线的距离公式,得"])
        self.steps.append(["", BaseEq([d, juli]).printing()])
        juli2 = point_to_line_dis2(args[0], args[1])
        juli2 = simplify(juli2)
        self.steps.append(["", "求得点到直线的距离的平方为%s" % (new_latex(juli2))])
        self.output.append(BaseValue(juli2))
        return self


# 求平行线的距离
class ParallelLineDis(BaseFunction):
    """
    直线:x+y-5=0与直线:2x+2y+9=0的距离为().
    """
    def solver(self, *args):
        a1, b1, c1 = args[0].sympify()
        a2, b2, c2 = args[1].sympify()
        if a1 > a2:
            beishu = a1 / a2
            a1 = a2
            b1 = b2
            c1 = c1 / beishu
        elif a1 < a2:
            beishu = a2 / a1
            c2 = c2 / beishu
        juli = Abs(c1 - c2) / sqrt(a1 ** 2 + b1 ** 2)
        self.steps.append(["", "两平行直线的距离为 %s" % (new_latex(juli))])
        self.output.append(BaseValue(juli))
        self.label.add("求两平行直线的距离")
        return self


if __name__ == '__main__':
    pass
